package com.leetcode.algorithm.y22.m07;

import com.leetcode.algorithm.common.TreeNode;

/**
 * 98. 验证二叉搜索树
 * 
 * https://leetcode.cn/problems/validate-binary-search-tree/
 * 
 * @author jie.deng
 *
 */
class Question0098Solution01 {

	public boolean isValidBST(TreeNode root) {
		return isValidBST(root, null, null);
	}

	private boolean isValidBST(TreeNode root, Integer minValue, Integer maxValue) {
		if (root == null) {
			return true;
		}
		if (minValue != null && root.val <= minValue) {
			return false;
		}
		if (maxValue != null && root.val >= maxValue) {
			return false;
		}
		return isValidBST(root.left, minValue, root.val) && isValidBST(root.right, root.val, maxValue);
	}

}